package com.cy.util;

import com.mongodb.WriteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/20.
 */
@Service
public class MongodbUtil {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存
     * @param object
     */
    public void insert(Object object, String name){
        mongoTemplate.insert(object, name);
    }

    /**
     * 查询
     * @param map
     */
    public List getData(Map<String, Object> map, Class claxx, String name){
        Query query = new Query();
        Criteria criteria = new Criteria();
        if (map != null && !map.isEmpty()){
            for (Map.Entry<String, Object> m : map.entrySet()){
                String key = m.getKey();
                if ("nowTime".equals(key))
                    continue;
                Object val = m.getValue();
                criteria.and(key).is(val);
            }
        }
        query.addCriteria(criteria);
        return mongoTemplate.find(query, claxx, name);
    }

    /**
     * 查询时间大于
     * @param map
     * @param claxx
     * @param name
     * @return
     */
    public List getAfterTimeData(Map<String, Object> map, Class claxx, String name){
        Query query = new Query();
        Criteria criteria = new Criteria();
        if (map != null && !map.isEmpty()){
            for (Map.Entry<String, Object> m : map.entrySet()){
                String key = m.getKey();
                Object val = m.getValue();
                if ("nowTime".equals(key)){
                    criteria.and(key).gte(val);
                } else {
                    criteria.and(key).is(val);
                }
            }
        }
        query.addCriteria(criteria);
        return mongoTemplate.find(query, claxx, name);
    }

    /**
     * 根据类型删除数据
     * @param map
     */
    public void delte(Map<String, Object> map, String name){
        if (map == null || map.isEmpty())
            return;
        Query query = new Query();
        Criteria criteria = new Criteria();
        for (Map.Entry<String, Object> m : map.entrySet()){
            String key = m.getKey();
            Object val = m.getValue();
            criteria.and(key).is(val);
        }
        query.addCriteria(criteria);
        WriteResult x = mongoTemplate.remove(query, name);
    }

    /**
     * 更新数据
     * @param map
     */
    public void update(Map<String, Object> map, String name, Update update){
        if (map == null || map.isEmpty())
            return;
        Query query = new Query();
        Criteria criteria = new Criteria();
        for (Map.Entry<String, Object> m : map.entrySet()){
            String key = m.getKey();
            Object val = m.getValue();
            criteria.and(key).is(val);
        }
        query.addCriteria(criteria);
        mongoTemplate.updateFirst(query, update, name);
    }

    /**
     * 插入更新数据
     * @param map
     */
    public void updateInsert(Map<String, Object> map, String name, Update update){
        if (map == null || map.isEmpty())
            return;
        Query query = new Query();
        Criteria criteria = new Criteria();
        for (Map.Entry<String, Object> m : map.entrySet()){
            String key = m.getKey();
            Object val = m.getValue();
            criteria.and(key).is(val);
        }
        query.addCriteria(criteria);
        mongoTemplate.upsert(query, update, name);
    }

    public static void main(String[] args) {
//        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//        MongodbUtil mongodbUtil = (MongodbUtil)ac.getBean("mongodbUtil");
//        QuartzService.minHandle(mongodbUtil);
////        MongoZJMinPojo MongoZJMinPojo = new MongoZJMinPojo();
////        Calendar now = Calendar.getInstance();
////        MongoZJMinPojo.setYear(now.get(Calendar.YEAR));
////        MongoZJMinPojo.setMonth(now.get(Calendar.MONTH) + 1);
////        MongoZJMinPojo.setDay(now.get(Calendar.DAY_OF_MONTH));
////        MongoZJMinPojo.setHour(now.get(Calendar.HOUR_OF_DAY));
////        MongoZJMinPojo.setMin(now.get(Calendar.MINUTE));
////        MongoZJMinPojo.setSecond(now.get(Calendar.SECOND));
////        save(MongoZJMinPojo);
//        Map<String, Object> map = new HashMap<>();
//        List<MongoZJMinPojo> list = getData(map, MongoZJSecondPojo.class, MongoNamePojo.SECOND);
//        System.out.println(list);
//
    }

}
